package com.mygdx.game.desktop.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

/**
 * 加载等待页面
 * 
 * @author Administrator
 *
 */
public class LoadingTestGame extends Game {

	private Stage stage;
	private Sprite font_load;
	private Image bg;

	private int[] hbx, hby, hbs, hba;
	private Image[] hb;
	private float[] hbr;
	private boolean[] hbab;
	private int floada = 100;
	private boolean floadb = true;

	private boolean flag = false;
	private List<String> status = new ArrayList<String>();

	@Override
	public void create() {
		stage = new Stage();
		// 加载字幕图片
		font_load = new Sprite(new Texture(Gdx.files.internal("images/load/font_load.png")));
		/*
		 * 纹理过滤方式（TextureFilter）: 图片在缩小时，一些相象就丢失了，在放大时，就需要填充一些像素 1、最近点采样
		 * GL_NEAREST-效果最差，速度快 2、线性纹理过滤(双线性过滤)GL_LINEAR-效果较好，计算量较大 3、mipmap纹理过滤(三线性过滤)
		 * GL_LINEAR_MIPMAP_LINEAR-效果最好，计算量最大 4、各向异性过滤
		 */
		font_load.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
		font_load.setSize(200, 25);
		font_load.setPosition(stage.getWidth() - font_load.getWidth() - 70, 30);

		bg = new Image(new Texture(Gdx.files.internal("images/load/bg.png")));
		bg.setPosition(0, 0);
		bg.setSize(stage.getWidth(), bg.getHeight());
		bg.setColor(1, 1, 1, 0);
		stage.addActor(bg);
		if (bg.getActions().size == 0) {
			// bg.addAction(Actions.delay(5f)); // 延时 单位s
			// bg.addAction(Actions.fadeIn(1f)); // 淡入
			// bg.addAction(Actions.fadeOut(0f)); // 淡出

			SequenceAction alpha = Actions.sequence(Actions.delay(1f), Actions.fadeIn(1f), Actions.fadeOut(0.5f)); // 顺序执行action
			bg.addAction(Actions.forever(alpha));
		}

		hbx = new int[10];
		hby = new int[10];
		hbs = new int[10];
		hba = new int[10];
		hbr = new float[10];
		hbab = new boolean[10];
		hb = new Image[10];
		// 初始化各个花瓣
		for (int i = 0; i < hbx.length; i++) {
			hb[i] = new Image(new Texture(Gdx.files.internal("images/load/f" + (new Random().nextInt(6) + 1) + ".png")));
			hbx[i] = 0;
			hby[i] = 40;
			hbs[i] = 0;
			hba[i] = 0;
			hbab[i] = true;
			// 添加花瓣到舞台
			stage.addActor(hb[i]);
		}
	}

	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		vary();

		stage.act();
		stage.draw();

		SpriteBatch batch = (SpriteBatch) stage.getBatch();
		batch.begin();
		font_load.draw(batch);
		batch.end();
	}

	@Override
	public void dispose() {
		stage.dispose();
	}

	public static void main(String[] args) {
		new LwjglApplication(new LoadingTestGame());
	}

	/**
	 * 变化
	 */
	public void vary() {
		// 获取透明度
		float globalAlpha = bg.getColor().a;
//		if(floadb) floada-=2; else floada+=2;
//		if((floada==2 && floadb)||(floada==150 && !floadb)) floadb=!floadb;
		font_load.setColor(1, 1, 1, globalAlpha);
		for (int i = 0; i < hbx.length; i++) {
			if (hbab[i]) {
				hbx[i] = (int) stage.getWidth() - new Random().nextInt(120) - 40; // x
				hby[i] = new Random().nextInt(80) + 10;	// y
				hbr[i] = 0;	// 旋转
				hbab[i] = false;	// 标志
				hbs[i] = new Random().nextInt(7) + 2;
				hba[i] = 255;
			}
			if (hby[i] > -50) {
				hbx[i] -= hbs[i];
				hby[i] -= hbs[i];
				hbr[i] += 1 / hbs[i];
				hba[i] -= hbs[i];
			} else
				hbab[i] = true;
			hb[i].setX(hbx[i]);
			hb[i].setY(hby[i]);
			hb[i].setRotation(hbr[i]);
			hb[i].setColor(1, 1, 1, globalAlpha * 0.5f);
		}
	}
	
}
